﻿using Peer;

using System;
using System.Collections.Generic;
using System.Runtime.CompilerServices;

namespace MicroMq.Helper
{
    internal class PeerConnection
    {

        private Dictionary<string, string> topic = new Dictionary<string, string>();

        internal readonly PeerStream peerStream;

        internal string this[string topic] => this.topic[topic];

        public Guid Guid => peerStream.Id;
 
        public MqConnectionManager ConnectionManager { get; set; }

        internal PeerConnection(PeerStream peerStream)
        {
            this.peerStream = peerStream;
        }

        public bool Is(string topicId)
        {
            if (this.topic.ContainsKey(topicId))
                return true;
            return false;
        }
        /// <summary>
        /// 添加当前链接的主题和分组
        /// </summary>
        /// <param name="topicId"></param>
        /// <param name="groupId"></param>
        /// <returns></returns>
        public PeerConnection AddGroup(string topicId, string groupId)
        {
            this.topic[topicId] = groupId;
            return this;
        }

        public override int GetHashCode()
        {
            return Guid.GetHashCode();
        }

        public override bool Equals(object obj)
        {
            if (base.Equals(obj))
            {
                return true;
            }
            if (obj is PeerConnection peer)
            {
                return peer.Guid == Guid;
            }
            return false;
        }

        ~PeerConnection()
        {
            peerStream.Dispose();
            GC.SuppressFinalize(this);
        }

    }

}
